from datetime import datetime
from ..db_conn_pool import conn_pool
import pymysql

class SellerInfo:
    def __init__(self, seller_id, country, postal_code, province, city, detailed_address, seller_name, seller_store_link):
        self.seller_id = seller_id
        self.country = country
        self.postal_code = postal_code
        self.province = province
        self.city = city
        self.detailed_address = detailed_address
        self.seller_name = seller_name
        self.seller_store_link = seller_store_link

    @classmethod
    def save_seller_info(cls, conn, seller_id, country, postal_code, province, city, detailed_address, seller_name, seller_store_link):
        cursor = conn.cursor()
        sql = '''
        INSERT INTO seller_infos (
            seller_id, country, postal_code, province, city, detailed_address, seller_name, seller_store_link
        ) VALUES (
            %s, %s, %s, %s, %s, %s, %s, %s
        )
        '''
        values = (
            seller_id,
            country,
            postal_code,
            province,
            city,
            detailed_address,
            seller_name,
            seller_store_link
        )
        try:
            cursor.execute(sql, values)
            conn.commit()
        except pymysql.err.IntegrityError:
            pass
        return cls(
            seller_id,
            country,
            postal_code,
            province,
            city,
            detailed_address,
            seller_name,
            seller_store_link
        )
        cursor.close()

    @classmethod
    def get_seller_info(cls, conn, seller_id):
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        try:
            sql = 'SELECT * FROM seller_infos WHERE seller_id = %s'
            cursor.execute(sql, (seller_id,))
            result = cursor.fetchone()
            if result:
                return cls(
                    result['seller_id'],
                    result['country'],
                    result['postal_code'],
                    result['province'],
                    result['city'],
                    result['detailed_address'],
                    result['seller_name'],
                    result['seller_store_link'],
                    result['created_at'],
                    result['updated_at']
                )
            else:
                return None
        except Exception as e:
            raise e
        finally:
            cursor.close()